package com.hk.aefz.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hk.aefz.po.*;
import com.hk.aefz.service.*;
import com.hk.aefz.vo.PageResult;
import com.hk.aefz.vo.PublishBlog;
import com.hk.aefz.vo.ResultMsg;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author 阿尔法哲
 * @since 2020-07-21
 */
@RestController
@RequestMapping("/essay")
public class EssayController {

    @Autowired
    private EssayService essayService;

    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private LabelService labelService;

    @Autowired
    private SpecialService specialService;

    @Autowired
    private CommentService commentService;

    @ApiOperation("写博客页面")
    @GetMapping(path = "/writing-blog")
    public ModelAndView writing() {
        ModelAndView mv = new ModelAndView("/blogafter/writingBlog");
//        1、查询文章标签
        List<Label> labels = labelService.list();
//        2、查询网站分类
        List<Classify> classifies = classifyService.list();
//        3、查询个人分类 获取个人账号id
        final Integer aid = (Integer) SecurityUtils.getSubject().getSession().getAttribute("aid");
        QueryWrapper<Special> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("aid",aid)
                .eq("sstate",0) //通过审核
                .eq("dedid",0);//未回收分类
        List<Special> specials = specialService.list(queryWrapper);
//        4、保存到request域中
        mv.addObject("labels",labels);
        mv.addObject("classifys",classifies);
        mv.addObject("specials",specials);
        return mv;
    }

    @ApiOperation("发布博客")
    @PostMapping(path = "/publish-blog")
    public ResultMsg publishBlog(@RequestBody PublishBlog publishBlog) {
        ResultMsg resultMsg = essayService.saveBlog(publishBlog);
        return resultMsg;
    }

    @ApiOperation("跳转到文章管理")
    @GetMapping(path = "/admin-essay")
    public ModelAndView adminEssay(@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "size",defaultValue = "5") Integer size) {
        ModelAndView mv = new ModelAndView("/blogafter/adminEssay");
        //        创建一个变量定义未删除状态
        final int dedid = 0;
//        1、分页查询所有未回收文章
        PageResult<Essay> pageEssay = essayService.selectList(page,size,dedid);
//        2、将查询到的数据存储到Request域中
        mv.addObject("pageEssay",pageEssay);
//        System.out.println("查询的数据："+pageEssay);
        return mv;
    }

    @ApiOperation("主页显示,通过条件分页查询")
    @PostMapping(path = "/select-blog")
    public PageResult<Essay> selectBlog(
            Integer cid,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "size",defaultValue = "5") Integer size)
    {
        //        1、分页查询一周内的
        PageResult<Essay> pageEssay = essayService.selectBlog(cid,page,size);
        return pageEssay;
//        return null;
    }

    @ApiOperation("博客详情")
    @GetMapping(path = "/select-blogdetails")
    public ModelAndView blogdetails(Integer eid) {
        ModelAndView mv = new ModelAndView("/blogafter/blogdetails");
        PageResult<Essay> pageResult = essayService.selectById(eid);
        mv.addObject("pageResult",pageResult);
        return mv;
    }


    @ApiOperation("逻辑删除,放入回收站")
    @GetMapping(path = "/del-essay")
    public ResultMsg delEssay(Integer eid,Integer dedid) {
        ResultMsg resultMsg = essayService.delEssay(eid,dedid);
        return resultMsg;
    }

    @ApiOperation("回收文章查询")
    @GetMapping(path = "/recycle-essay")
    public PageResult<Essay> recycleEssay(@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "size",defaultValue = "5") Integer size) {
        //        创建一个变量定义已删除状态
        final int dedid = 1;
//        1、分页查询所有未回收文章
        PageResult<Essay> pageEssay = essayService.selectList(page,size,dedid);
        return pageEssay;
    }

    @ApiOperation("公开/私有文章查询")
    @GetMapping(path = "/open-essay")
    public PageResult<Essay> openEssay(@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "size",defaultValue = "5") Integer size,Integer status ) {
//        1、分页查询所有公开或私有文章
        PageResult<Essay> pageEssay = essayService.selectOpenList(page,size,status);
        return pageEssay;
    }

    @ApiOperation("修改文章类型")
    @PostMapping(path = "/update-open-or-private")
    public ResultMsg updateOpenOrPrivate(Essay essay) throws IOException {
        System.out.println(essay);
        ResultMsg resultMsg = new ResultMsg();
        boolean b = essayService.updateById(essay);
        if (b) {
            resultMsg.setState(200);
            resultMsg.setMsg("OK");
        } else {
            resultMsg.setState(500);
            resultMsg.setMsg("Error");
        }
        return resultMsg;
    }


    @ApiOperation("永久删除")
    @GetMapping(path = "/delete-essay")
    public ResultMsg deleteEssay(Integer eid) {
        ResultMsg resultMsg = new ResultMsg();
        boolean removeById = essayService.removeById(eid);
//        通过id查询当前文章所有评论，并获取评论ids集合
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<Comment>().eq("eid", eid);
        List<Integer> cmids = commentService.list(queryWrapper).stream().map(Comment::getCmid).collect(Collectors.toList());
//        删除该文章所有相关评论
        boolean b = commentService.removeByIds(cmids);
        if (removeById && b) {
            resultMsg.setState(200);
            resultMsg.setMsg("该文章已永久删除!");
        } else {
            resultMsg.setState(500);
            resultMsg.setMsg("删除失败，请重新操作!");
        }
        return resultMsg;
    }


}

